$(function(){
	
	/*
	 * picker对象
	 */
	var Picker=function(el,pickerbox){
		/*属性*/
		this.el=el;
		this.pickerBox=pickerbox+" ";
		
		/*公用*/
		var that=this;
		var doc=document;

		doc.querySelector(this.el).addEventListener("touchend",function(){
			that.showPicker();
		});
		
		var cancelEl=this.pickerBox+".picker-cancel";
		doc.querySelector(cancelEl).addEventListener("touchend",function(){
			that.closePicker();
		});
		
		var confirmEl=this.pickerBox+".picker-confirm";
		doc.querySelector(confirmEl).addEventListener("touchend",function(){
			that.closePicker();
		});
		
		/*方法*/
		//初始化
		this.updataValue=function(){
			//更新input的值
			var pickerBd=this.pickerBox+".picker-bd";
			var activeEl=this.pickerBox+".swiper-slide-active";
			
			doc.querySelector(pickerBd).addEventListener("touchend",function(){
				//设置文本值
				var pickerActive=doc.querySelector(activeEl);
				var txt=pickerActive.innerText;
				var currentInput=doc.querySelector(that.el);
				currentInput.value=txt;
				
				//设置value
				var value=pickerActive.getAttribute("value");
				if(value){ currentInput.setAttribute("value",value);currentInput.setAttribute("name",value); }
			});
		};
		
		//显示
		this.showPicker=function(el){
			var el=el || that.pickerBox;
			//设置body的overflow，阻止页面滚动
			doc.querySelector("body").style.cssText="width:100vw; height:100vh; overflow:hidden";
			doc.querySelector(el).style.display="block";
		};
		
		//隐藏
		this.closePicker=function(el){
			var el=el || that.pickerBox;
			doc.querySelector(el).style.display="none";
			doc.querySelector("body").style.overflow="auto";
		};
		
		this.updataValue();
	};
	
	//初始化滑动事件
	var pickerSwiper = new Swiper('.swiper-container', {
	    direction: 'vertical',
	    loop: false,
	    observer:true,//修改swiper自己或子元素时，自动初始化swiper
    	observeParents:true,//修改swiper的父元素时，自动初始化swiper
	});
	
	var cityPicker=new Picker("#city",".cityPicker");
	var roundPicker=new Picker("#round",".roundPicker");
	var industryPicker=new Picker("#industry",".industryPicker");
	var currencyPicker=new Picker("#currency",".currencyPicker");
	
	//提交表单
	$(".confirm").on("touchend",function(){
		//数据
		var projectName=$("#proName").val().trim(),//项目名称
			business=$("#industry").attr("value"),//领域
			round=$("#round").attr("value"),//轮次
			city=$("#city").attr("value"),//地区
			equity=$("#equity").val().trim(),//出让股权
			money=$("#money").val().trim(),//融资金额
			currency=$("#currency").attr("value"),//金额单位
			txt=$("#profile").val().trim(), //简介
			id=$("#id").val().trim(), //Id
			openId=$("#openId").val().trim(), //openId
			userId=$("#userId").val().trim(), //userId扫码ID
			deliveryId=$("#deliveryId").val().trim(); //userId接收者ID
		
		var areaId = 0;
		var roundId = 0;
		var fieldId = 0;
		
		var roundName = $("#round").attr("name");
		var areaName = $("#city").attr("name");
		var fieldName = $("#industry").attr("name");
		areaId = (areaName == '' && areaName == null) ? areaId=city : areaId=areaName;
		roundId = (roundName == '' && roundName == null) ? roundId=round : roundId=roundName;
		fieldId = (fieldName == '' && fieldName == null) ? fieldId=business : fieldId=fieldName;
			
		//验证
		if(projectName==="" || projectName===null){
			$(".errTip").html("请输入项目名称！").slideDown(300);
			return false;
		}
		else if(projectName.length>15){
			$(".errTip").html("项目名称字数15字以内！").slideDown(300);
			return false;
		}
		else if(!Re.name.test(projectName.charAt(0))){
			$(".errTip").html("项目名称首字母要求任意中文、英文、数字！").slideDown(300);
			return false;
		}
		
		if(city==="" || city===null){
			$(".errTip").html("请选择所在的城市！").slideDown(300);
			return false;
		}
		
		if(business==="" || business===null){
			$(".errTip").html("请选择行业！").slideDown(300);
			return false;
		}
		
		if(round==="" || round===null){
			$(".errTip").html("请选择轮次！").slideDown(300);
			return false;
		}
		
		if(equity==="" || equity===null){
			$(".errTip").html("请填写项目该轮次出让的股权数！").slideDown(300);
			return false;
		}else if(isNaN(equity) || equity<=0 || equity>=100){
			$(".errTip").html("请输入0-100之间的实数！").slideDown(300);
			return false;
		}
		
		if(money==="" || money===null){
			$(".errTip").html("请输入融资金额！").slideDown(300);
			return false;
		}else if(isNaN(money) || money<1 || money>999999){
			$(".errTip").html("请填写1-999999以内的数字！").slideDown(300);
			return false;
		}
		
		if(currency==="" || currency===null){
			$(".errTip").html("请选择币种！").slideDown(300);
			return false;
		}
		
		if(txt==="" || txt===null){
			$(".errTip").html("请填写字数1-500字以内的项目简介！").slideDown(300);
			return false;
		}
		
		$(".errTip").slideUp(300);
		
		var unit = (currency=='人民币') ? 1 :2;
		
		//发送数据
		$.ajax({  
			type: "POST",  
			url: "/scanCode/createProject",  
			dataType: 'json',
			data: {
					"id":id,
					"name":projectName,
					"userId":userId,
					"areaId":areaId,
					"fieldsId":fieldId,
					"roundId":roundId,
					"stock":equity,
					"money":money,
					"unit":unit,
					"openId":openId,
					"proDetail":txt,
					"deliveryId":deliveryId
			},  
			success: function(data){
				alert(data.status);
				if(data.status==true){
					location.href='/scanCode/success';
				} else {
					location.href='/scanCode/error';
				}
			},  
			error: function(res){  
			}  
		});
		
	});
});